package com.xq.spring.javacfg;

import com.xq.spring.beans.Axe;
import com.xq.spring.beans.Chinese;
import com.xq.spring.beans.Person;
import com.xq.spring.beans.StoneAxe;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 使用Java的方式配置容器中的Bean(XML配置的替代方式).
 * 
 * @author 郭凯强
 */
@Configuration
@ImportResource("classpath:beans-listener.xml")
public class BeanConfig {
	/**
	 * `@Value`修饰Field
	 */
	@Value("孙悟空")
	private String personName;

	/**
	 * 配置名称为chinese的Bean.如果@Bean没有注明名称,则方法名为新生成的Bean的名称
	 * 
	 * @return 名称为chinese的Bean
	 */
	@Bean(name="chinese")
//	@Lazy(true)
	public Person person() {
		// instantiate, configure and return bean ...
		Chinese chinese = new Chinese();
		chinese.setName(personName);
		chinese.setAxe(stoneAxe());
		return chinese;
	}

	@Bean(name = "stoneAxe")
	public Axe stoneAxe() {
		return new StoneAxe();
	}
}
